﻿using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Browser;
using DeepEarth;
using DeepEarth.Controls;
using DeepEarth.Geometry;
using DeepEarth.Provider.BlueMarble;
using DeepEarth.Provider.GoogleMap;
using DeepMap.MapTokenServiceReference;
using DeepEarth.Layers;

namespace DeepMap
{
	public partial class Page 
	{
        string TileSourceMode = "Web";
        MapTokenServiceClient client = new MapTokenServiceClient();

        private ReverseGeocode _ReverseGeocodeBehavior;
		public Page()
		{
			// 为初始化变量所必需
			InitializeComponent();
            if (HtmlPage.IsEnabled)
            {
                GmTileSource gs;
                _ReverseGeocodeBehavior = new ReverseGeocode(map);
                client.GetTileSourceModeCompleted += (o, e) =>
                    {
                        TileSourceMode = e.Result;
                        switch (TileSourceMode)
                        {
                            case "Local":
                                gs = new GmTileSource(GmMapModes.GoogleMapLocal);
                                break;
                            
                            default:
                                gs = new GmTileSource(GmMapModes.GoogleMapWeb);
                                break;
                        }
                        map.BaseLayer.Source = gs;
                        map.SetViewCenter(new Point(114.2578125, 30.6000938735501), 4);
                        if (TileSourceMode == GmMapModes.GoogleMapWebAndLocal.ToString())
                        {
                            map.BaseLayer.Source.InitializeCompleted += new System.EventHandler(Source_InitializeCompleted);
                        }
                    };
                client.GetTileSourceModeAsync();

                
            }
		}

        void Source_InitializeCompleted(object sender, System.EventArgs e)
        {
            if (sender is SenderGmArgs)
            {
                string url = ((SenderGmArgs)sender).Url;
                string zoom = ((SenderGmArgs)sender).Zoom;
                string x = ((SenderGmArgs)sender).TilePositionX;
                string y = ((SenderGmArgs)sender).TilePositionY;
                
                client.SaveImageCachCompleted += (o, a) =>
                {
                    string message = a.Result;
                };
                client.SaveImageCachAsync(url, zoom, x, y);
            }

        }
		
		private void ToggleSidePanel_Click(object sender, RoutedEventArgs e)
        {
            sidePanelControl.ToggleSidePanel();
        }

	}
}